package com.example.cdctest.service.impl;

import com.example.cdctest.mapper.TestPaperStructureMapper;
import com.example.cdctest.pojo.Category;
import com.example.cdctest.pojo.Examination;
import com.example.cdctest.pojo.TestPaperStructure;
import com.example.cdctest.service.TestPaperStructureService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class TestPaperStructureServiceImpl implements TestPaperStructureService
{
    @Resource
    private TestPaperStructureMapper testPaperStructureMapper;

    @Override
    public void insertTestPaperStructure(String[] categordId, String[] categorySum, Integer examinationId) {
        List<TestPaperStructure> testPaperStructures=new ArrayList<>();
        TestPaperStructure testPaperStructure=null;
        Examination examination=null;
        Category category=null;
        for (int i = 0; i < categordId.length; i++) {
            for (int j = 0; j < categorySum.length; j++) {
                if(i==j){
                    testPaperStructure=new TestPaperStructure();
                    examination=new Examination();
                    examination.setId(examinationId);
                    category=new Category();
                    category.setId(Integer.parseInt(categordId[i]));
                    testPaperStructure.setExamination(examination);
                    testPaperStructure.setCategory(category);
                    testPaperStructure.setQuestionCategoryCount(Integer.parseInt(categorySum[j]));
                }
            }
            testPaperStructures.add(testPaperStructure);
        }
        TestPaperStructure structure=null;
        for (TestPaperStructure paperStructure : testPaperStructures) {
            //先查询是否有满足条件的设置
            try {
                structure=testPaperStructureMapper.selectTestPaperStructure(paperStructure);
            } catch (Exception e) {
                structure=null;
            }
            if(structure!=null){    //如果有，更新
                testPaperStructureMapper.updateTestPaperStructure(paperStructure);
            }else {        //如果没有,添加
                testPaperStructureMapper.insertTestPaperStructure(paperStructure);
            }
        }
    }

    @Override
    public void insert(TestPaperStructure object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(TestPaperStructure object) {

    }

    @Override
    public TestPaperStructure findById(Integer id) {
        return null;
    }

    @Override
    public List<TestPaperStructure> findAll() {
        return null;
    }

    @Override
    public PageInfo<TestPaperStructure> findPage(int pageSize) {
        return null;
    }
}
